local gameRule = fk.CreateSkill {
    name = "ty_1v2_gamerule&",
}

local can_trigger = function(self, event, target, player, data)
    return (target == player) or (target == nil)
end

gameRule:addEffect(fk.DrawInitialCards, {
    priority = 0.001,
    can_trigger = can_trigger,
    on_trigger = function(self, event, target, player, data)
        local room = player.room
        if player.role == "lord" and player:getMark("chengzhu") == "ty_1v2_xz&" then
            data.num = data.num + 3
            room:addPlayerMark(player, MarkEnum.AddMaxCards, 3)
        end
    end,
})

--兵尽
gameRule:addEffect(fk.TurnEnd, {
    priority = 0.001,
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        return target == player and player.role ~= "lord" and player.room:getBanner("RoundCount") > 5
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:loseHp(player, 1, gameRule.name)
    end,
})

--粮绝
gameRule:addEffect(fk.DrawNCards, {
    priority = 0.001,
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        return target == player and player.role == "lord" and player.room:getBanner("RoundCount") > 5
    end,
    on_use = function(self, event, target, player, data)
        data.n = data.n - 1
    end,
})

--歃血
gameRule:addEffect(fk.DetermineDamageCaused, {
    priority = 0.001,
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        return target == player and player:getMark("@ty_1v2_shaxue") > 0 and data.to.role == player.role and
            data.to ~= player
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        data:preventDamage()
        room:removePlayerMark(player, "@ty_1v2_shaxue", 1)
    end,
})
--歃血失效
gameRule:addEffect(fk.RoundStart, {
    priority = 0.001,
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        local n = 0
        for _, id in ipairs(player.room.alive_players) do
            if id:getMark("@ty_1v2_shaxue") > 0 then
                n = n + 1
            end
        end
        return player.room:getBanner("RoundCount") > 2 and n > 0
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        for _, id in ipairs(room.alive_players) do
            if id:getMark("@ty_1v2_shaxue") > 0 then
                room:setPlayerMark(player, "@ty_1v2_shaxue", 0)
            end
        end
    end,
})

--第八轮结束时，若城主还健在，城主获胜
gameRule:addEffect(fk.RoundEnd, {
    priority = 0.001,
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        return player.room:getBanner("RoundCount") > 8
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:gameOver("lord")
    end,
})

return gameRule
